package com.tfswx.jcyw.sqlmodel.api.bean.table;


import lombok.Data;

/**
 * @author 张述江
 * @date 2023/9/15
 */
@Data
public class OrderByElement {

    private Expression expression;
    private boolean asc = true;
    private NullOrdering nullOrdering;

    public OrderByElement(Expression expression, boolean asc) {
        this.expression = expression;
        this.asc = asc;
    }

    public OrderByElement(Expression expression) {
        this(expression, true);
    }

    public OrderByElement() {
        this(null);
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append(expression.toString());

        b.append(" ").append(asc ? "ASC" : "DESC");

        if (nullOrdering != null) {
            b.append(' ').append(nullOrdering == NullOrdering.NULLS_FIRST ? "NULLS FIRST" : "NULLS LAST");
        }
        return b.toString();
    }


    public enum NullOrdering {
        NULLS_FIRST, NULLS_LAST;

        public static NullOrdering from(String ordering) {
            return Enum.valueOf(NullOrdering.class, ordering.toUpperCase());
        }
    }
}
